const {get_teacher_info} = require('../../../Class/get_teacher_info.js');
const {get_others} = require('../../../Class/GetOthers.js');
const teacherInfo = new get_teacher_info();
const getOthers = new get_others();
Page({
  data: {
    ScreenHeight: "",
    navbarHeight: '',
    tabs: ['个人资料', '教育经历', '教师成就','课程视频'],
    selected: 0,
  },

  onLoad(options){
    const {tabid,teacherid} = options
    const navbarHeight = getOthers.Set_navbar()
    this.setData({
      tabid,teacherid,navbarHeight
    })
  },
  
  async onReady() {
    wx.showLoading({
      title: '数据通信中',
      mask: true
    })
    const { tabid, teacherid } = this.data
    const teacherinfo = await teacherInfo.GoTeacherInfo(parseInt(tabid), parseInt(teacherid))
    const replaceTags = (str) => str.replace(/<b>(.*?)<\/b>/g, "<strong>$1</strong>").replace(/\\n/g, '<br>').replace(/&nbsp;/g,'<br>');
    let past = replaceTags(teacherinfo.past);
    let achievement = replaceTags(teacherinfo.achievement);
    this.setData({
      teacherinfo,
      achievement,
      past,
      section1Height: 0,
      section2Height: 0,
      section3Height: 0
    });
    const query = wx.createSelectorQuery().in(this);
    const sections = ['#section1', '#section2', '#section3','#section4'];
    sections.forEach((section, index) => {
      query.select(section).boundingClientRect((res) => {
        const heightKey = `section${index + 1}Height`;
        this.setData({
          [heightKey]: res.height
        });
      }).exec();
    });
    wx.hideLoading()
  },

  ChangeTab: function (e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      selected: index,
    });
    if (index == 0) {
      this.setData({
        toView: 'section1'
      })
    } else if (index == 1) {
      this.setData({
        toView: 'section2'
      })
    } else if (index == 2) {
      this.setData({
        toView: 'section3'
      })
    } else if (index == 3){
      this.setData({
        toView:'section4'
      })
    }
  },

  GoBack() {
    wx.navigateBack({
      delta: 1
    })
  },

  onScrollViewScroll: function (event) {
    // 获取当前滚动位置
    let scrollTop = event.detail.scrollTop;
    var h1 = this.data.section1Height
    var h2 = this.data.section2Height
    var h3 = this.data.section3Height
    var h4 = this.data.section4Height
    if (scrollTop >= 0 && scrollTop < h1) {
      this.setData({
        selected: 0
      });
    } else if (scrollTop >= h1 && scrollTop < h1 + h2) {
      this.setData({
        selected: 1
      });
    } else if (scrollTop >= h1 + h2 && scrollTop < h1 + h2 + h3) {
      this.setData({
        selected: 2
      });
    } else if(scrollTop >= h1 + h2 + h3 && scrollTop < h1 + h2 + h3 + h4) {
      this.setData({
        selected : 3
      })
    }
  },
  // 跳转视频页面
  Video(e){
    const {id} = e.currentTarget.dataset
    const video_info = JSON.stringify(this.data.teacherinfo.lesson[id])
    wx.navigateTo({
      url: '/pages/OnlineClass/classvideo/classvideo?video_info='+ video_info,
    })
  },
  
})